package com.zzwtec.web.controller;

import com.zzwtec.ResultObject;
import com.zzwtec.SimpleResult;
import com.zzwtec.basedata.bean.Area;
import com.zzwtec.basedata.services.AreaService;
import com.zzwtec.web.exception.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author  草原狼
 *
 * 2018年8月29日
 */
@Controller
@RequestMapping("api/area")
public class APIAreaController {
    @Autowired
    private AreaService areaService;

    /**
     * 添加区域界面
     * @return

    @GetMapping("/add")
    public String addAreaUI() {
        return "/area/area_add";
    }
     */

    /**
     * 添加区域业务
     * @return
     */
    @RequestMapping(value="/add", method=RequestMethod.POST)
    @ResponseBody
    public SimpleResult addArea(@ModelAttribute Area bean) {
        SimpleResult result = areaService.addArea(bean);
        return result;
    }

    /**
     * 删除区域
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ResponseBody
    public SimpleResult removeArea(@PathVariable("id") String id, ModelMap model) {
        SimpleResult result = areaService.removeArea(id);
        return result;
    }

    /**
     * 编辑区域
     * @return

    @GetMapping("/edit/{id}")
    public String modifyAreaUI(@PathVariable("id") String id,ModelMap model) {
        ResultObject<Area> result = areaService.queryAreaById(id);
        if(result.getCode() == 0){
            model.put("area", result.getData());
        }else{
            model.put("msg", result.getMsg());
        }
        return "/area/area_edit";
    }
     */

    /**
     * 提交编辑小区
     * @param id
     * @param bean
     * @return
     */
    @PostMapping("/upd")
    @ResponseBody
    public SimpleResult modifyArea(@ModelAttribute Area bean ,ModelMap model) {
        model.put("msg", "修改区域失败");
        SimpleResult result = areaService.modifyArea(bean);
        if(result.getCode() == 0){
            model.put("msg", "修改区域成功");
        }
        //return "redirect:/area/list";
        return result;

    }
    @GetMapping("/list")
    @ResponseBody
    public ResultObject queryAllArea(ModelMap model) {
        ResultObject<List<Area>> result = areaService.queryAllArea();
        //model.put("list", result.getData());
        //return "/area/area_list";
        return result;
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResultObject queryAreaById(@PathVariable("id") String id,ModelMap model) {
        ResultObject<Area> result = areaService.queryAreaById(id);
        //model.put("area", result.getData());
        //return "/area/area_view";
        return result;
    }

    @RequestMapping("/hello")
    public String hello() throws Exception {
        throw new Exception("发生错误");
    }

    @RequestMapping("/json")
    public String json() throws MyException {
        throw new MyException("发生错误2");
    }
}
